import 'package:boss/textStyle/text_style.dart';

import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

import 'package:flustars/flustars.dart' as flustars;
import 'package:router/annotation.dart';

@RouterConfig(AddUnitPage.pageName)
class AddUnitPage extends StatefulWidget {
  static const pageName = '/AddUnitPage';

  @Autowired()
  final List<String> units;

  const AddUnitPage(this.units);

  @override
  _AddUnitPageState createState() => _AddUnitPageState();
}

class _AddUnitPageState extends BaseState<AddUnitPage> {
  _AddUnitPageState() : super(AddUnitPage.pageName);

  String unit;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '添加单位',
        actions: [
          LabelButton(
            title: '保存',
            padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
            textStyle: TextStyleConstant().blue_28,
            onTap: () {
              if (flustars.TextUtil.isEmpty(unit)) {
                toast('请输入单位名称');
                return;
              }

              if (unit.length > 20) {
                toast('单位名称长度不高于20');
                return;
              }

              if (widget.units.contains(unit)) {
                toast('和已有的单位名称重复');
                return;
              }

              Navigator.pop(context, unit);
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.symmetric(horizontal: 30.w),
        child: BaseTextFormField(
          prefixText: '单位名称',
          hintText: '请输入',
          maxLength: 20,
          onChanged: (value) {
            unit = value;
          },
        ),
      ),
    );
  }
}
